package com.yg.controller;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yg.config.result.R;
import com.yg.entity.singleEntity.QualityTeacher;
import com.yg.entity.singleEntity.QualityTeam;
import com.yg.service.QualityTeacherService;
import com.yg.service.QualityTeamService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 对应sheet06，此表是质量工程团队表，对应表中的团队部分 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/qualityTeam")
@CrossOrigin
@Api(tags = "对应第六张表")
public class QualityTeamController {

    @Autowired
    private QualityTeamService qualityTeamService;


    /**
     * 添加质量工程团队
     *
     * @param qualityTeam
     * @return
     */
    @ApiOperation("添加质量工程团队")
    @PostMapping("postQualityTeam")
    public R postTeacher(
            @ApiParam(name = "质量工程团队数据") @RequestBody(required = true) QualityTeam qualityTeam
    ) {
        boolean flag = qualityTeamService.save(qualityTeam);

        return flag ? R.OK() : R.ERROR().message("添加失败");
    }

    /**
     * 获得所有质量工程团队信息
     *
     * @param current 当前页
     * @param limit   每页限制的条目
     * @return
     */
    @ApiOperation("获得所有质量工程团队信息")
    @GetMapping("getAllQualityTeam")
    public R getAllTeacher(
            @ApiParam(name = "current", value = "当前页", required = true) @RequestParam(name = "current") long current,
            @ApiParam(name = "limit", value = "每页显示的条目数", required = true) @RequestParam(name = "limit") long limit
    ) {
        try {
            Page<QualityTeam> page = new Page<>(current, limit);

            QueryWrapper<QualityTeam> wrapper = new QueryWrapper<>();
            wrapper.eq("deleted", 0);

            qualityTeamService.page(page, wrapper);
            List<QualityTeam> list = page.getRecords();
            long total = page.getTotal();
            long pages = page.getPages();

            return R.OK().data("qualityTeamList", list).data("total", total).data("pages", pages);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询失败");
        }
    }

    /**
     * 条件查询质量工程团队数据
     *
     * @param current     当前页
     * @param limit       每页限制的条目
     * @param qualityTeam 查询条件
     * @return
     */
    @ApiOperation("条件查询质量工程团队数据")
    @PostMapping("selectQualityTeamByCondition")
    public R selectTeacherByCondition(
            @ApiParam(name = "current", value = "当前页", required = false) @RequestParam(name = "current") long current,
            @ApiParam(name = "limit", value = "每页显示的条目数", required = false) @RequestParam(name = "limit") long limit,
            @ApiParam(name = "qualityTeam", value = "质量工程团队表", required = false) @RequestBody(required = false) QualityTeam qualityTeam
    ) {
        try {
            Page<QualityTeam> page = new Page<>(current, limit);

            QueryWrapper<QualityTeam> wrapper = new QueryWrapper<>();
            wrapper.eq("deleted", 0);
            if (!StringUtils.isEmpty(qualityTeam.getName())) {
                wrapper.like("name", qualityTeam.getName());
            }

            if (!StringUtils.isEmpty(qualityTeam.getLevel())) {
                wrapper.like("level", qualityTeam.getLevel());
            }

            qualityTeamService.page(page, wrapper);
            List<QualityTeam> list = page.getRecords();
            long total = page.getTotal();
            long pages = page.getPages();

            return R.OK().data("qualityTeamList", list).data("total", total).data("pages", pages);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询失败");
        }

    }

    /**
     * 质量工程团队修改
     *
     * @param qualityTeam
     * @return
     */
    @ApiOperation("质量工程团队修改")
    @PutMapping("putQualityTeam")
    public R putTeacher(
            @ApiParam(name = "qualityTeam", value = "修改后的团队数据", required = false) @RequestBody QualityTeam qualityTeam
    ) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id", qualityTeam.getId());
        boolean flag = qualityTeamService.update(qualityTeam, wrapper);
        return flag ? R.OK() : R.ERROR().message("修改失败");
    }

    /**
     * 删除质量工程团队信息
     *
     * @param qualityTeam
     * @return
     */
    @ApiOperation("删除质量工程团队信息")
    @DeleteMapping("deleteQualityTeam")
    public R deleteTeacher(
            @ApiParam(name = "qualityTeam", value = "团队数据", required = false) @RequestBody QualityTeam qualityTeam
    ) {

        String id = null;
        try {
            id = qualityTeam.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询信息错误");
        }

        try {
            boolean flag = qualityTeamService.removeById(id);
            return flag ? R.OK() : R.ERROR();
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("删除失败");
        }
    }
}

